/**
 * <p>Title: LeaderBoard.java</p>
 * <p>Description: CS 343 Final Project. This class will set up a leaderboard to keep track of high scores</p>
 * @author Tim Olmstead, Brady Fetting, Erik Brown, Thales Kirk
 * @email timothy.olmstead@my.uwrf.edu, brady.fetting@my.uwrf.edu, 
 * 			erik.brown@my.uwrf.edu, thales.kirk@my.uwrf.edu
 * @date November 15, 2013
 */

package GameState;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import Components.MoveableBackground;
import Components.Record;
import Components.SoundEffects;
import Main.Begin;
import Settings.Config;

public class LeaderBoard extends JPanel implements ActionListener {

	private JButton backToMainMenu;
	private JScrollPane leadScroll;
	private MoveableBackground background;
	private Image filterImage = new ImageIcon("src/images/filter.png")
			.getImage();
	private Image logo = new ImageIcon("src/images/supersegway3000.png")
			.getImage();
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public LeaderBoard() {
		setLayout(null);
		background = new MoveableBackground(2000);
		backToMainMenu = new JButton(new ImageIcon("src/images/backButton.png"));

		// backToMainMenu.setRolloverIcon(new
		// ImageIcon("src/images/PlayHover.png"));
		backToMainMenu.setBounds(25, Config.FRAME_HEIGHT - 128 - 40, 128, 128);

		// removes background of button
		backToMainMenu.setContentAreaFilled(false);
		// removes button border
		backToMainMenu.setBorder(BorderFactory.createEmptyBorder());
		backToMainMenu.addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent arg0) {
			}

			public void mouseEntered(MouseEvent arg0) {
				(new SoundEffects()).playTickSound();
			}

			public void mouseExited(MouseEvent arg0) {
			}

			public void mousePressed(MouseEvent arg0) {
			}

			public void mouseReleased(MouseEvent arg0) {
			}

		});

		backToMainMenu.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Begin.app.setGameState(0);
			}

		});
		add(backToMainMenu);

		List<Record> scores = Begin.app.getDatabase().getRecords();
		String[][] data = new String[scores.size()][4];
		for (int i = 0; i < scores.size(); i++) {
			data[i][0] = i + 1 + "";
			data[i][1] = scores.get(i).getUsername();
			data[i][2] = scores.get(i).getScore() + "";
			data[i][3] = scores.get(i).getMph() + "";
		}
		JTable leaderboardTable = new JTable(data, new String[] { "Rank",
				"Username", "Score", "Top Speed" });
		leaderboardTable.setOpaque(false);
		leadScroll = new JScrollPane(leaderboardTable);
		leadScroll.setBackground(Color.red);
		leadScroll.setOpaque(false);
		leadScroll.setBounds(250, 300, 500, 200);
		add(leadScroll);

	}

	/**
	 * @param g
	 * @description paints the graphics of the main menu items and images
	 */
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		g2d.drawImage(background.getBlue(), 0, 0, null);

		g2d.drawImage(background.getBackClouds1(),
				background.getX1backClouds(), background.getY1backClouds(),
				null);
		g2d.drawImage(background.getBackClouds2(),
				background.getX2backClouds(), background.getY2backClouds(),
				null);

		g2d.drawImage(background.getForeClouds1(),
				background.getX1foreClouds(), background.getY1foreClouds(),
				null);
		g2d.drawImage(background.getForeClouds2(),
				background.getX2foreClouds(), background.getY2backClouds(),
				null);

		// background
		if (background.getX1() < Config.FRAME_WIDTH)
			g2d.drawImage(background.getBackgroundImage1(), background.getX1(),
					background.getY1(), null);
		if (background.getX2() < Config.FRAME_WIDTH)
			g2d.drawImage(background.getBackgroundImage2(), background.getX2(),
					background.getY2(), null);

		g2d.drawImage(filterImage, 0, 0, null);
		g2d.drawImage(logo, (Config.FRAME_WIDTH - logo.getWidth(null)) / 2, 40,
				null);

	}

	/**
	 * @param arg0
	 * @description repaints for the leaderboard
	 */
	public void actionPerformed(ActionEvent arg0) {
		repaint();
	}
}
